load("02_05_m5s_meetup_vote_analysis_fb_targets_wt_fb_activity.RData")
load('02_05_m5s_meetup_vote_analysis_spatial_model_wt_fb.RData')

require(sp)

page_by_province <- 
  as.data.frame(table(targets$PROVINCIA_2013))
page_by_province <-
  merge(page_by_province, italy_map_2013, by.x = "Var1", by.y = 'PROVINCIA')

require(ggplot)
p1 <-
  ggplot(page_by_province, aes(y=Freq , x=voters)) +
  geom_point() +
  scale_x_log10() +
  scale_y_log10() +
  annotation_logticks() +
  geom_smooth(method='lm', se = FALSE) +
  labs(x = 'Voters in the 2013 election', y = 'Number of Facebook pages')

cor.test(page_by_province$Freq, 
         page_by_province$voters)

require(dplyr)
likes_by_province <- 
  targets %>%
  group_by(PROVINCIA_2013) %>%
  summarize(likes = sum(likes))
likes_by_province <-
  merge(likes_by_province, italy_map_2013, by.x = "PROVINCIA_2013", by.y = 'PROVINCIA')
p2 <-
ggplot(likes_by_province, aes(y=likes , x=voters)) +
  geom_point() +
  scale_x_log10() +
  scale_y_log10() +
  annotation_logticks() +
  geom_smooth(method='lm', se = FALSE) +
  labs(x = 'Voters in the 2013 election', y = 'Total likes of Facebook pages (in 2016)')

cor.test(likes_by_province$likes, 
         likes_by_province$voters)

# ggplot(as.data.frame(italy_map_2013), aes(x=days_30liking_users, y=votes)) +
#   geom_point() +
#   scale_x_log10() +
#   scale_y_log10() +
#   annotation_logticks() + 
#   geom_smooth(method='lm', se = FALSE)
# 
# summary(lm(data = as.data.frame(italy_map_2013), log(votes) ~ log(days_30liking_users)))

require(ggplot2)
require(rgdal)
require(ggplot2)
require(scales)
require(plyr)
require(maptools)

italy_box_y <- c(36, 47.5)
italy_box_x <- c(6.1, 19.5)

italy_map_2013@data$id = rownames(spTransform(italy_map_2013, CRS("+proj=longlat +datum=WGS84"))@data)
italy_map_2013.points = fortify(italy_map_2013, region="id")
italy_map_2013.df = join(italy_map_2013.points, italy_map_2013@data, by="id")

italy_map <- spTransform(italy_map_2013, CRS("+proj=longlat +datum=WGS84"))
italy_map@data$id = rownames(italy_map_2013@data)
gpclibPermit() #avoid "Error: isTRUE(gpclibPermitStatus()) is not TRUE" in the next command
italy_map.points = fortify(italy_map,region="id")
italy_map.df = join(italy_map.points, italy_map@data, by="id")

# MAP

circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  return(data.frame(x = xx, y = yy))
}

turin <- circleFun(c(7.7000, 45.0667),.6, npoints = 100)
milan <- circleFun(c(9.183333, 45.466667),.6, npoints = 100)
rome <- circleFun(c(12.5, 41.9),.6, npoints = 100)
naples <- circleFun(c(14.25, 40.833333),.6, npoints = 100)
venice_padua <- circleFun(c(12.117251, 45.359166),.6, npoints = 100)

p0 <- 
ggplot() + 
  geom_polygon(data=italy_map.df, 
            aes(long, lat, group=group), fill="white") +
  geom_point(data = targets, aes(x=lon, y=lat), alpha = 0.45, color ='red', size = 0.2) +
  labs(x=NULL,y=NULL) +
  theme(axis.text=element_blank(),axis.ticks=element_blank()) +
  coord_cartesian(xlim = italy_box_x, ylim = italy_box_y ) + 
  labs(x = 'Facebook pages with a local scope') +
  geom_path(data=turin, aes(x=x,y=y), size=.3) +
  geom_text(aes(7.7000, 45.0667, label="Turin"), family="Palatino",
            hjust=.8, vjust=-1.5, size=3) +
  geom_path(data=milan, aes(x=x,y=y), size=.3) +
  geom_text(aes(9.183333, 45.466667, label="Milan"), family="Palatino",
            hjust=.5, vjust=-4, size=3) +
  geom_path(data=rome, aes(x=x,y=y), size=.3) +
  geom_text(aes(12.5, 41.9, label="Rome"), family="Palatino",
            hjust=1, vjust=1.5, size=3) +
  geom_path(data=naples, aes(x=x,y=y), size=.3) +
  geom_text(aes(14.25, 40.833333, label="Naples"), family="Palatino",
            hjust=1, vjust=1.5, size=3) +
  geom_path(data=venice_padua, aes(x=x,y=y), size=.3) +
  geom_text(aes(12.117251, 45.359166, label="Venice/Padua"), family="Palatino",
            hjust=-.2, vjust=.4, size=3)
